package exam.controller;

import com.github.pagehelper.PageInfo;
import exam.bean.House;
import exam.bean.Student;
import exam.dao.HouseDao;
import exam.service.IHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("houses")
public class HouseController {
    @Autowired
    private IHouseService houseService;

    @RequestMapping(value = "/house", method = {RequestMethod.POST,RequestMethod.GET})
    public String queryStudents(@RequestParam(name = "pageNum", required = false, defaultValue = "1")
                                int pageNum,
                                @RequestParam(name = "pageSize", required = false, defaultValue = "10")
                                int pageSize, @RequestParam Map<String,Object> queryMap, Model model, HttpServletRequest request) {
        //开始分页
        //PageHelper.startPage(pageNum,pageSize);

        List<House> houseList = houseService.selectHouse(pageNum, pageSize);

        model.addAttribute("pageInfo",new PageInfo<>(houseList));
        model.addAttribute("houseList",houseList);
        return "indexHouse";

    }
    @ResponseBody
    @PostMapping("/add")
    public boolean add(House house){

        return houseService.insertHouse(house)>0?true:false;

    }
    @GetMapping("/toAdd")
    public String toAdd(){

        return "addHouse";
    }
    @GetMapping("/toUpdate")
    public String toUpdate(int id,Model model){

        Map<String,Object> map=new HashMap<String,Object>();
        map.put("id",id);

        House house = houseService.selectHouseById(map);

        model.addAttribute("house",house);

        return "updateHouse";
    }

    @ResponseBody
    @PostMapping("/update")
    public boolean update(House house){

        return houseService.updateHouse(house)>0?true:false;

    }

    @ResponseBody
    @GetMapping("/delete")
    public boolean delete(int id){
        return houseService.deleteHouse(id)>0?true:false;
    }

    @GetMapping("/toDetail")
    public String toDetail(int id,Model model){

        Map<String,Object> map=new HashMap<String,Object>();
        map.put("id",id);

        House house = houseService.selectHouseById(map);

        model.addAttribute("house",house);

        return "detailHouse";
    }


}
